package kava.framework.lcdui;

import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

import kava.framework.util.GUIUtil;
import kava.framework.util.Util;

public class Widget {
	public final static int STATE_FOCUSED = 1;
	public final static int STATE_PRESSED = 2;
	public final static int STATE_UNVISIBLE = 3;
	public final static int STATE_NORMAL = 0;
	// the original point is the parent form's left - top corner
	public int x;
	public int y;
	public int width;
	public int height;
	public int state;
	public int ID;
	public String text;

	public int bgColor = Form.COLOR_NONE;
	public String bgImage;
	public String fgImage;
	public int fgColor;

	public int focusBgColor = Form.COLOR_NONE;
	public String focusBgImage;
	public String focusFgImage;
	public int focusFgColor;

	public int pressedBgColor;
	public String pressedBgImage;
	public String pressedFgImage;
	public int pressedFgColor;

	public boolean visual = true;
	public int fontSize = Font.SIZE_SMALL;

	public ContainerForm parent;

	public Widget(ContainerForm parent) {
		this.parent = parent;
		this.fgColor = parent.getFgColor();
	}

	public Widget(int ID, int x, int y, int width, int height, String text) {
		this.ID = ID;
		this.x = x;
		this.y = y;
		this.width = width;
		this.height = height;
		this.text = text;
	}

	public boolean cover(int x, int y) {
		return Util.testPointInRect(x, y, this.x, this.y, this.width,
				this.height);
	}

	public void paintImage(String imageName, Graphics g) {

		int px = parent.x;
		int py = parent.y;

		if (imageName != null) {
			if (imageName.indexOf('#') >= 0) {
				int pos = imageName.indexOf('(');
				String realImageName = imageName.substring(0, pos).trim();
				String clip = imageName.substring(pos + 1,
						imageName.length() - 1);
				String[] rec = Util.split(clip, "#");
				int r0 = Integer.parseInt(rec[0]);
				int r1 = Integer.parseInt(rec[1]);
				int r2 = Integer.parseInt(rec[2]);
				int r3 = Integer.parseInt(rec[3]);
				int xx = px + x + (width - r2) / 2;
				int yy = py + y + (height - r3) / 2;
				g.setClip(xx, yy, r2, r3);
				Image img = parent.getPngFromPngRes(realImageName);
				if (img != null) {
					g.drawImage(img, xx - r0, yy - r1, Graphics.TOP
							| Graphics.LEFT);
				} else {
					Util.debug("Widget.paintImage", "FATAL ERROR : can't load "
							+ imageName);
				}

				KavaFrame.resetClip(g);

			} else if (imageName.endsWith("(|)")) {
				String imageName2 = imageName.substring(0,
						imageName.length() - 3);
				Image image = parent.getPngFromPngRes(imageName2);
				if (image != null) {
					int iw = image.getWidth();
					int ih = image.getHeight();
					int ix = px + x + (width - iw) / 2;
					int iy = py + y + (height - ih) / 2;
					GUIUtil.drawConvertedImage(g, image, ix, iy, true);
				} else {
					Util.debug("Widget.paintImage", "FATAL ERROR : can't load "
							+ imageName);
				}
			} else if (imageName.endsWith("(-)")) {
				String imageName2 = imageName.substring(0,
						imageName.length() - 3);
				Image image = parent.getPngFromPngRes(imageName2);
				if (image != null) {
					int iw = image.getWidth();
					int ih = image.getHeight();
					int ix = px + x + (width - iw) / 2;
					int iy = py + y + (height - ih) / 2;
					GUIUtil.drawConvertedImage(g, image, ix, iy, false);
				} else {
					Util.debug("Widget.paintImage", "FATAL ERROR : can't load "
							+ imageName);
				}
			} else {
				Image image = parent.getPngFromPngRes(imageName);
				if (image != null) {
					int iw = image.getWidth();
					int ih = image.getHeight();
					int ix = px + x + (width - iw) / 2;
					int iy = py + y + (height - ih) / 2;
					g.drawImage(image, ix, iy, Graphics.TOP | Graphics.LEFT);
				} else {
					Util.debug("Widget.paintImage", "FATAL ERROR : can't load "
							+ imageName);
				}
			}

		}
		KavaFrame.resetClip(g);

	}

	private void paintState(int _bgColor, String _bgImage, String _fgImage,
			int _fgColor, int px, int py, Graphics g) {
		if (_bgColor != Form.COLOR_NONE) {
			g.setColor(_bgColor);
			g.fillRect(px + x, py + y, width, height);
		}

		// Util.debug("Widget.paintState", "_bgImage=
		// "+_bgImage+",_fgImage="+_fgImage);
		paintImage(_bgImage, g);
		paintImage(_fgImage, g);
		if (text != null) {
			g.setColor(_fgColor);
			Font font = g.getFont();
			font = Font.getFont(font.getFace(), font.getStyle(), this.fontSize);
			g.drawString(text, px + x + width / 2, py + y
					+ (height - font.getHeight()) / 2, Graphics.TOP
					| Graphics.HCENTER);

		}

	}

	public void paint(Graphics g) {
		if (this.visual == false) {
			return;
		}
		int px = parent.x;
		int py = parent.y;
		switch (state) {
		case STATE_NORMAL:
			paintState(bgColor, bgImage, fgImage, fgColor, px, py, g);
			break;
		case STATE_FOCUSED:

			paintState(focusBgColor, focusBgImage, focusFgImage, focusFgColor,
					px, py, g);

			break;
		case STATE_PRESSED:
			paintState(pressedBgColor, pressedBgImage, pressedFgImage,
					pressedFgColor, px, py, g);

			break;
		}

	}

}
